import logging

import pytest

from resource.pom.contacts import Contacts


@pytest.fixture(autouse=True)
def preset_contactor(device):
    logging.info('方法前置----预置联系人')
    Contacts.create_contactor(device)
    yield
    logging.info('方法后置----删除联系人')
    Contacts.delete_contactor(device)


class Test:
    app = Contacts

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        "新建联系人，输入姓名。公司。手机号能正常保存"
        logging.info('步骤1.联系人列表中点击联系人进入')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.menu_contacts)
        device.refresh_layout()
        test_contact = device.get_element_by_text(self.app.test_name)
        device.click_element(test_contact)
        logging.info('预期结果1.可以正常进入联系人应用内')
        device.refresh_layout()
        favor = device.get_element_by_text('收藏')
        assert favor, '联系人应用内没有收藏控件'

        logging.info('步骤2.点击左下角"星号"标识')
        device.click_element(favor)
        logging.info('预期结果2."星号"标识变为黑色，名称变为取消收藏')
        device.refresh_layout()
        device.assert_text_exist('取消收藏')

        logging.info('步骤3.退出联系人，切换到收藏界面并查看')
        device.go_back()
        device.click(*self.app.menu_favor)
        logging.info('预期结果3.正常切换收藏页面，收藏界面显示已收藏的联系人信息')
        device.refresh_layout()
        device.assert_text_exist(self.app.test_name)

        logging.info('步骤4、点击进入已收藏联系人详情页')
        device.click(*self.app.detail_btn)
        logging.info('预期结果4、正常进入联系人详情页')
        device.refresh_layout()
        device.assert_text_exist('取消收藏')

        logging.info('步骤5、点击取消收藏')
        device.click_element(favor)
        logging.info('预期结果5、取消成功，按钮变为"星号"收藏按钮')
        device.refresh_layout()
        device.assert_text_exist('收藏')
